﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SocialLearning
{
    public partial class GroupDeleteForm : Form
    {
        #region Constructor

        public GroupDeleteForm()
        {
            InitializeComponent();
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Loads the CBX data.
        /// </summary>
        private void LoadCbxData()
        {
            List<string> groupList = UAC_GroupManagement.Instance.SeeGroup().ToList();
            cbxDeleteGroup.Items.Clear();
            foreach (string groupname in groupList)
            {
                cbxDeleteGroup.Items.Add(groupname);
            }
        }

        /// <summary>
        /// Removes the group.
        /// </summary>
        /// <param name="name">The name.</param>
        private void DeleteGroup(string name)
        {
            try
            {
                UAC_GroupManagement.Instance.RemoveGroup(name);
                cbxDeleteGroup.Text = "";
                LoadCbxData();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion

        #region Private Controls

        /// <summary>
        /// Handles the Click event of the btnDelete control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(cbxDeleteGroup.Text))
            {
                MessageBox.Show("Please enter a group name!", "Information",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                DeleteGroup(cbxDeleteGroup.Text);
                MessageBox.Show(string.Format("{0} group was deleted.", cbxDeleteGroup.Text),
                            "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        /// <summary>
        /// Close the form
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// DeleteGroup
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void GroupDeleteForm_Load(object sender, EventArgs e)
        {
            LoadCbxData();
        }
        #endregion
    }
}
